<template>
    <div class="addProd" ref="doms" id="doms">
        <div class="box flex-b">
            <div class="left">
                <div class="prod-title flex-a" id="basic">
                    <div class="basic">基本信息<span>引用上一次填写</span></div>
                    <el-button type="warning" style="padding:12px 50px" class="btn-plain"
                        @click="back">返回上一页</el-button>
                </div>
                <div class="prod-content">
                    <el-form ref="form" :model="form">
                        <div>
                            <p>活体品种</p>
                            <el-select v-model="form.varId" placeholder="请选择活体品种" class="input">
                                <el-option v-for="item in pzList" :key="item.id" :label="item.name" :value="item.id">
                                </el-option>
                            </el-select>
                            <!-- <div class="tip">(没找到品种？请 <span @click="addBrand">新增品种</span>)</div> -->
                        </div>
                        <div>
                            <p>活体名称</p>
                            <el-input v-model="form.name" class="input" placeholder="请输入活体名称" clearable></el-input>
                        </div>
                        <!-- <div>
                            <el-checkbox v-model="checked">批量添加宠物</el-checkbox>
                            <el-tooltip class="item" effect="dark" content="宠物数量生成相应的活体数据，活体名称格式为：活体名称+序号，如：仓鼠01"
                                placement="top">
                                <i class="el-icon-question"></i>
                            </el-tooltip>
                            <el-input v-if="checked" v-model="form.name" style="margin-left:10px" class="input"
                                placeholder="请输入宠物数量" clearable></el-input>
                        </div> -->
                        <div>
                            <p>进货价</p>
                            <el-input v-model="form.inPrice" class="input input-radius" placeholder="请输入进货价" clearable>
                                <template slot="prepend">￥</template>
                            </el-input>
                        </div>
                        <div>
                            <p>零售价</p>
                            <el-input v-model="form.outPrice" class="input input-radius" placeholder="请输入零售价" clearable>
                                <template slot="prepend">￥</template>
                            </el-input>
                        </div>
                        <div>
                            <p>性别</p>
                            <el-select v-model="form.sex" placeholder="请选择性别" class="input">
                                <el-option v-for="item in sexList" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                        <div>
                            <p>毛色</p>
                            <el-input v-model="form.petColor" class="input" placeholder="请输入毛色" clearable></el-input>
                        </div>
                        <div>
                            <p>体重</p>
                            <el-input v-model="form.weight" class="input input-radius-right" placeholder="请输入体重"
                                clearable>
                                <template slot="append">kg</template>
                            </el-input>
                        </div>
                        <div>
                            <p>肩高/体长</p>
                            <el-input v-model="form.high" class="input input-radius-right" placeholder="请输入肩高/体长"
                                clearable>
                                <template slot="append">cm</template>
                            </el-input>
                        </div>
                        <div>
                            <p>出生日期</p>
                            <el-date-picker class="input" v-model="form.date" value-format="yyyy-MM-dd" type="date"
                                placeholder="选择日期">
                            </el-date-picker>
                        </div>
                    </el-form>
                </div>
                <div class="prod-title flex-a" id="more">
                    <div class="basic">更多信息</div>
                </div>
                <div class="prod-content">
                    <el-form ref="form" :model="form">
                        <div>
                            <p>活体状态</p>
                            <el-select v-model="form.state" placeholder="请选择活体状态" class="input">
                                <el-option v-for="item in stateList" :key="item.value" :label="item.label"
                                    :value="item.value+''">
                                </el-option>
                            </el-select>
                        </div>
                        <div>
                            <p>到店日期</p>
                            <el-date-picker class="input" v-model="form.inDate" value-format="yyyy-MM-dd" type="date"
                                placeholder="选择日期">
                            </el-date-picker>
                        </div>
                        <div>
                            <p>活体来源</p>
                            <el-input v-model="form.source" class="input" placeholder="请输入活体来源" clearable></el-input>
                        </div>
                        <!-- <div>
                            <p>备注</p>
                            <el-input v-model="form.name" class="input" placeholder="请输入备注" clearable></el-input>
                        </div> -->
                        <div>
                            <p>活体图片</p>
                            <el-upload class="avatar-uploader" :action="action" :headers="headers"
                                :show-file-list="false" :on-success="handleAvatarSuccess">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </div>
                    </el-form>
                </div>
                <div>
                    <el-button type="primary" style="padding:12px 71px" class="btn" @click="onSubmit">提交</el-button>
                    <el-button type="warning" style="padding:12px 50px" class="btn-plain" @click="back">返回上一页</el-button>
                </div>
            </div>
            <div class="right">
                <ul>
                    <li :class="tab == 'basic' ? 'active' : ''" @click="jumpTo('basic')">基本信息</li>
                    <li :class="tab == 'more' ? 'active' : ''" @click="jumpTo('more')">更多信息</li>
                </ul>
            </div>
        </div>
        <!-- 新增品种弹窗 -->
        <el-dialog title="新增品种" :visible.sync="sortDialogVisible" width="25%" class="center">
            <el-form :model="sortForm" :rules="sortRules" label-width="100px" class="input">
                <el-form-item label="品种名称" prop="name">
                    <el-input v-model="sortForm.name" autocomplete="off" placeholder="请输入品种名称"></el-input>
                </el-form-item>
                <el-form-item label="品种类型" prop="name">
                    <el-select v-model="sortForm.type" placeholder="请选择品种类型" class="input" style="width:100%">
                        <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button class="btn" style="padding:12px 70px" @click="sortDialogVisible = false">提 交</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { getVarList } from "@/api/pet/var"
import { addPetInfo,getPetInfo,updatePetInfo } from "@/api/pet/petInfo"
export default {
    name: "addProd",
    components: {

    },
    data() {
        return {
            id: null,
            imageUrl: '',
            form: {
                ck1: false,
                ch3: false
            },
            pzList: [],
            sexList: [
                {
                    value: 1,
                    label: '弟弟'
                },
                {
                    value: 2,
                    label: '妹妹'
                }
            ],
            action: process.env.VUE_APP_BASE_API + '/pet/upload/comFile',
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            stateList: [
                {
                    value: 0,
                    label: '未出售'
                },
                {
                    value: 1,
                    label: '已预定'
                },
                {
                    value: 2,
                    label: '已赠送'
                },
                {
                    value: 3,
                    label: '已夭折'
                },
                {
                    value: 4,
                    label: '已调拨'
                },
                {
                    value: 5,
                    label: '待领养'
                },
                {
                    value: 6,
                    label: '已领养'
                }
            ],
            typeList: [
                {
                    value: 0,
                    label: '猫咪'
                },
                {
                    value: 1,
                    label: '狗狗'
                },
                {
                    value: 2,
                    label: '小宠'
                },
                {
                    value: 3,
                    label: '其他'
                }
            ],
            checked: false,
            // 富文本编辑器配置
            editorOption: {
                modules: {
                    toolbar: [
                        ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线
                        ['blockquote', 'code-block'], // 引用  代码块
                        [{ header: 1 }, { header: 2 }], // 1、2 级标题
                        [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表
                        [{ script: 'sub' }, { script: 'super' }], // 上标/下标
                        [{ indent: '-1' }, { indent: '+1' }], // 缩进
                        [{ direction: 'rtl' }], // 文本方向
                        [{ size: ['12', '14', '16', '18', '20', '22', '24', '28', '32', '36'] }], // 字体大小
                        [{ header: [1, 2, 3, 4, 5, 6] }], // 标题
                        [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
                        // [{ font: ['songti'] }], // 字体种类
                        [{ align: [] }], // 对齐方式
                        ['clean'], // 清除文本格式
                        ['image', 'video'] // 链接、图片、视频
                    ]
                },
                placeholder: '请输入正文'
            },
            content: '',
            target: 0,
            tab: 'basic',
            sortDialogVisible: false,//新增分类弹窗
            srotDialogTitle: '',
            sortForm: {},
            sortRules: {
                name: [
                    { required: true, message: '请输入分类名称', trigger: 'blur' }
                ],
            },
        };
    },
    created() {
        this.getVarList()//获取品种下拉
        if (this.$route.query.id) {
            this.id = this.$route.query.id
            this.getPetDetail(this.$route.query.id)
        }
    },
    mounted() {
        window.addEventListener("scroll", this.handleScroll);
    },
    methods: {
        // 返回上一页
        back() {
            this.$router.push({ path: '/goods/petInfo' })
        },
        //获取活体详情
        getPetDetail(id) {
            getPetInfo(id).then(response => {
                this.form = response.data;
            });
        },
        //表单提交
        onSubmit() {
            if (this.id) {
                updatePetInfo(this.form).then(response => {
                    this.$modal.msgSuccess("编辑成功");
                    this.back()
                });
            } else {
                addPetInfo(this.form).then(response => {
                    this.$modal.msgSuccess("添加成功");
                    this.back()
                });
            }
        },
        //获取品种下拉
        getVarList() {
            getVarList().then(res => {
                if (res.code == 200) {
                    this.pzList = res.data
                }
            })
        },
        // 图片上传成功
        handleAvatarSuccess(res, file) {
            this.imageUrl = res.url;
            this.form.picture = res.url
        },
        //页面滚动
        jumpTo(ele) {
            this.tab = ele
            let targetbox = document.getElementById(ele);
            this.target = targetbox.offsetTop;
            document.documentElement.scrollTop = this.target
        },
        //页面滚动
        handleScroll() {
            let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
            var basicTop = document.querySelector("#basic").offsetTop
            var moreTop = document.querySelector("#more").offsetTop
            console.log(scrollTop, moreTop)
            if (scrollTop > moreTop) {
                this.tab = 'more'
            } else if (scrollTop > basicTop) {
                this.tab = 'basic'
            }
        },
        // 失去焦点事件
        onEditorBlur(quill) {
            console.log('editor blur!', quill)
        },
        // 获得焦点事件
        onEditorFocus(quill) {
            console.log('editor focus!', quill)
        },
        // 准备富文本编辑器
        onEditorReady(quill) {
            console.log('editor ready!', quill)
        },
        // 内容改变事件
        onEditorChange({ quill, html, text }) {
            console.log('editor change!', quill, html, text)
            this.content = html
        },
        //新增分类
        addSort() {
            this.srotDialogTitle = '新增分类'
            this.sortDialogVisible = true
        },
        //新增品牌
        addBrand() {
            this.srotDialogTitle = '新增品牌'
            this.sortDialogVisible = true
        },
        //新增供应商
        addSupplier() {
            this.srotDialogTitle = '新增供应商'
            this.sortDialogVisible = true
        }
    }
};
</script>
<style scoped lang="scss">
.box {
    position: relative;

    .left {
        width: 80%;
    }

    .right {
        ul {
            list-style: none;
            height: 300px;
            border-left: 2px solid #E4E7ED;
            position: fixed;
            right: 40px;
            top: 200px;
            padding-left: 0;
        }

        li {
            padding: 0 20px;
            height: 40px;
            box-sizing: border-box;
            line-height: 40px;
            list-style: none;
            font-size: 14px;
            font-weight: 500;
            color: #303133;
            position: relative;
            left: -2px;
            box-sizing: border-box;
            border-left: 2px solid transparent;
        }

        li:hover {
            cursor: pointer;
            color: #409eff;
        }

        li.active {
            color: #409eff;
            border-color: #409eff;
        }
    }

    .prod-title {
        .basic {
            font-size: 18px;
            color: #333336;
            font-weight: 700;

            span {
                color: #409eff;
                font-size: 12px;
                font-weight: normal;
                margin: 0 20px 0 5px;
            }
        }
    }

    .prod-content {
        margin-top: 20px;

        div {
            margin-bottom: 15px;

            .mr {
                margin-right: 10px;
            }

            p {
                color: #000000;
                font-size: 14px;

                span {
                    color: #b3b3b3;
                    font-size: 12px;
                }
            }

            .el-input {
                width: 240px;
            }

            .select {
                margin-left: 10px;
                width: 80px;
            }

            .el-textarea {
                width: 500px;
            }

            .el-button.btn {
                margin-left: 10px;
            }

            .tip {
                display: inline-block;
                font-size: 12px;
                color: #333336;
                margin-left: 5px;

                span {
                    color: #409eff;
                }

                span:hover {
                    cursor: pointer;
                }
            }

        }

        .flex {
            p {
                margin-top: 0 !important;
            }
        }

        .suggest {
            span {
                color: rgb(245, 108, 108);
                font-size: 12px;
            }
        }
    }
}
</style>